package cn.com.pusai.common.util;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.log4j.Logger;

import cn.com.pusai.common.config.ConfigParameter;

public class MailUtil {
	private static Logger log = Logger.getLogger(MailUtil.class);
	
	public static String formatSendContent(String key, Object[] parameter) {
		String content = ConfigParameter.Properties.getProperty(key);
		return String.format(content, parameter);
	}
	public static void sendMail(String toAdd,String subject, String content) throws Exception{
		sendMail(toAdd, subject, content, null);
	}
	
	public static void sendMail(String toAdd,String subject, String content, String attachment) throws Exception{
		log.info("接收到邮件发送请求，地址[" + toAdd + "]，内容：" + content);
		String host = ConfigParameter.Properties.getProperty("mail.send.host");
		final String username = ConfigParameter.Properties.getProperty("mail.send.username");
		final String password = ConfigParameter.Properties.getProperty("mail.send.password");
		String from = ConfigParameter.Properties.getProperty("mail.send.from");
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", ConfigParameter.Properties.getProperty("mail.send.auth")); 
		Session session = Session.getDefaultInstance(props, new Authenticator(){
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		}
		);
		session.setDebug(true);
		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.addRecipient(Message.RecipientType.TO,new InternetAddress(toAdd));
		message.setSubject(subject);
		message.setSentDate(new Date());
		message.setContent(content, "text/html;charset=gbk");
		if (attachment != null && !attachment.equals("")) {
			BodyPart messageBodyPart = new MimeBodyPart();
			Multipart multipart = new MimeMultipart();
			messageBodyPart.setContent(content, "text/html;charset=gbk");//给BodyPart对象设置内容和格�??/编码方式   
			multipart.addBodyPart(messageBodyPart);
			// 建立第二部分：附�??    
			messageBodyPart = new MimeBodyPart();
			// 获得附件
			DataSource source = new FileDataSource(attachment);
			//设置附件的数据处理器
			messageBodyPart.setDataHandler(new DataHandler(source));
			// 设置附件文件�??
			messageBodyPart.setFileName(source.getName());
			// 加入第二部分
			multipart.addBodyPart(messageBodyPart);
			// 将多部分内容放到e-mail�??
			message.setContent(multipart);
		}
		message.saveChanges();
		Transport transport = session.getTransport("smtp");
		transport.connect(host, username, password);
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
		log.info("邮件发�?�完毕，地址[" + toAdd + "]");
	}
	
	public static void main(String[] args) {
		try {
System.out.println(RandomStringUtils.randomAlphanumeric(16));
			//System.out.println(SecurityUtil.md5Hex("1"));
			sendMail("yanghui527@126.com","测试邮件","<html><body><a href='http://www.163.com'>测试邮件内容</a></body></html>");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
